package com.iplant.ams.serviceImpl.dao.dms.maintenance;

import com.iplant.ams.service.po.dms.maintenance.DMSMaintenanceItem;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

@DBBaseDAO(DMSMaintenanceItem.class)
public class DMSMaintenanceItemDAO extends BaseDAO {

    private static final Logger logger = LoggerFactory.getLogger(DMSMaintenanceItemDAO.class);

    private static DMSMaintenanceItemDAO Instance = null;

    private DMSMaintenanceItemDAO() {
        super("");
        this.Init();
    }

    public static DMSMaintenanceItemDAO getInstance() {
        if (Instance == null)
            Instance = new DMSMaintenanceItemDAO();
        return Instance;
    }

    public List<DMSMaintenanceItem> DMS_SelectMaintenanceItemList(BMSEmployee wLoginUser, int wDeviceID, OutResult<Integer> wErrorCode){
        Map<String,Object> wParams= new HashMap<>();
        wParams.put("DeviceID", wDeviceID);
        wParams.put("Active", 1);
        return this.SelectPOList(wLoginUser, wParams , DMSMaintenanceItem.class, wErrorCode);
    }


    public List<DMSMaintenanceItem> DMS_QueryMaintenanceItemListByStatus(int wDeviceID, int wConfigurationID,
                                                                         String wName, int wStatus,
                                                                         int wType, int wMaintenanceLevel, Pagination wPagination,
                                                                         OutResult<Integer> wErrorCode) {
        List<DMSMaintenanceItem> wResultList = new ArrayList<>();
        try {
            String wSQLText = StringUtils.Format("SELECT t.*,t1.Name CreatorName,t2.Name EditorName,t9.Code " +
                    "DeviceCode,t9" +
                    ".Name DeviceName,t4.Name ConfigurationName FROM {1}.dms_maintenance_item t "
                    + " left join {0}.mbs_user t1 on t.CreatorID=t1.ID "
                    + " left join {0}.mbs_user t2 on t.EditorID=t2.ID "
                    + " left join {1}.dms_device_ledger t3 on t.DeviceID=t3.ID "
                    + " left join {1}.dms_configuration t4 on t.ConfigurationID=t4.ID "
                    + " left join {1}.dms_maintenance_item_task t5 on t.ID=t5.MaintenanceItemID "
                    + " left join {1}.ams_asset_list t9 on t3.AssetInfoID=t9.ID "
                    + "WHERE 1=1"
                    + " and(:wDeviceID <=0 or t.DeviceID= :wDeviceID)"
                    + " and(:wMaintenanceLevel <=0 or t.MaintenanceLevel= :wMaintenanceLevel)"
                    + " and(:wType <=0 or t.Type= :wType)"
                    + " and(t5.Status = :wStatus)"
                    + " and(:wConfigurationID <=0 or t.ConfigurationID= :wConfigurationID)"
                    + " and(:wName is null or :wName = '' or t.Name like :wName)"
                    + " and(t.Active= 1)", DBSchemas.Basic.getDBName(), DBSchemas.AMS.getDBName());

            Map<String, Object> wParms = new HashMap<>();

            wParms.put("wDeviceID", wDeviceID);
            wParms.put("wType", wType);
            wParms.put("wConfigurationID", wConfigurationID);
            wParms.put("wName", StringUtils.isEmpty(wName) ? wName : "%" + wName + "%");
            wParms.put("wStatus", wStatus);
            wParms.put("wMaintenanceLevel", wMaintenanceLevel);

            List<Map<String, Object>> wQueryResultList = this.queryForList(wSQLText, wParms);

            SetValue(wResultList, wQueryResultList);
        } catch (Exception ex) {
            logger.error("DMS_QueryDMSMaintenanceItemListByStatus", ex);
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
        }
        return wResultList;
    }

    /**
     * 赋值
     **/
    private void SetValue(List<DMSMaintenanceItem> wResultList, List<Map<String, Object>> wQueryResultList) {
        try {
            DMSMaintenanceItem wDMSMaintenanceItem;
            for (Map<String, Object> wSqlDataReader : wQueryResultList) {
                wDMSMaintenanceItem = new DMSMaintenanceItem();

                wDMSMaintenanceItem.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
                wDMSMaintenanceItem.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
                wDMSMaintenanceItem.CreatorName = StringUtils.parseString(wSqlDataReader.get("CreatorName"));
                wDMSMaintenanceItem.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
                wDMSMaintenanceItem.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
                wDMSMaintenanceItem.EditorName = StringUtils.parseString(wSqlDataReader.get("EditorName"));
                wDMSMaintenanceItem.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
                wDMSMaintenanceItem.DeviceID = StringUtils.parseInt(wSqlDataReader.get("DeviceID"));
                wDMSMaintenanceItem.DeviceCode = StringUtils.parseString(wSqlDataReader.get("DeviceCode"));
                wDMSMaintenanceItem.DeviceName = StringUtils.parseString(wSqlDataReader.get("DeviceName"));
                wDMSMaintenanceItem.ConfigurationID = StringUtils.parseInt(wSqlDataReader.get("ConfigurationID"));
                wDMSMaintenanceItem.ConfigurationName = StringUtils.parseString(wSqlDataReader.get("ConfigurationName"
                ));
                wDMSMaintenanceItem.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
                wDMSMaintenanceItem.Description = StringUtils.parseString(wSqlDataReader.get("Description"));
                wDMSMaintenanceItem.LastMaintenanceTime = StringUtils.parseCalendar(wSqlDataReader.get(
                        "LastMaintenanceTime"));
                wDMSMaintenanceItem.WorkTime = StringUtils.parseDouble(wSqlDataReader.get("WorkTime"));
                wDMSMaintenanceItem.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));
                wDMSMaintenanceItem.OrderNum = StringUtils.parseInt(wSqlDataReader.get("OrderNum"));

                wResultList.add(wDMSMaintenanceItem);
            }
        } catch (Exception ex) {
            logger.error("SetValue", ex);
        }
    }

    public List<DMSMaintenanceItem> DMS_QueryMaintenanceItemListNoTask(int wDeviceID, int wConfigurationID,
                                                                       String wName,
                                                                       int wType, int wMaintenanceLevel, Pagination wPagination,
                                                                       OutResult<Integer> wErrorCode) {
        List<DMSMaintenanceItem> wResultList = new ArrayList<>();
        try {
            String wSQLText = StringUtils.Format("SELECT t.*,t1.Name CreatorName,t2.Name EditorName,t9.Code " +
                    "DeviceCode,t9" +
                    ".Name DeviceName,t4.Name ConfigurationName FROM {1}.dms_maintenance_item t "
                    + " left join {0}.mbs_user t1 on t.CreatorID=t1.ID "
                    + " left join {0}.mbs_user t2 on t.EditorID=t2.ID "
                    + " left join {1}.dms_device_ledger t3 on t.DeviceID=t3.ID "
                    + " left join {1}.dms_configuration t4 on t.ConfigurationID=t4.ID "
                    + " left join {1}.ams_asset_list t9 on t3.AssetInfoID=t9.ID "
                    + "WHERE 1=1"
                    + " and(:wDeviceID <=0 or t.DeviceID= :wDeviceID)"
                    + " and(:wMaintenanceLevel <=0 or t.MaintenanceLevel= :wMaintenanceLevel)"
                    + " and(:wType <=0 or t.Type= :wType)"
                    + " and(t.ID not in (SELECT MaintenanceItemID FROM {0}.dms_maintenance_item_task where " +
                    "MaintenanceItemID=t.ID and ( Status in (1,2,3) or (Status=4 and EditTime>:StartTime and " +
                    "EditTime<:EndTime) )))"
                    + " and(:wName is null or :wName = '' or t.Name like :wName)"
                    + " and(t.Active= 1)", DBSchemas.Basic.getDBName(), DBSchemas.AMS.getDBName());

            Map<String, Object> wParms = new HashMap<>();

            wParms.put("wType", wType);

            // 今日时间
            Calendar wTodaySTime = Calendar.getInstance();
            wTodaySTime.set(Calendar.HOUR_OF_DAY, 0);
            wTodaySTime.set(Calendar.MINUTE, 0);
            wTodaySTime.set(Calendar.SECOND, 0);
            Calendar wTodayETime = Calendar.getInstance();
            wTodayETime.set(Calendar.HOUR_OF_DAY, 23);
            wTodayETime.set(Calendar.MINUTE, 59);
            wTodayETime.set(Calendar.SECOND, 59);

            wParms.put("StartTime", wTodaySTime);
            wParms.put("EndTime", wTodayETime);

            wParms.put("wDeviceID", wDeviceID);
            wParms.put("wMaintenanceLevel", wMaintenanceLevel);
            wParms.put("wType", wType);
            wParms.put("wConfigurationID", wConfigurationID);
            wParms.put("wName", StringUtils.isEmpty(wName) ? wName : "%" + wName + "%");

            List<Map<String, Object>> wQueryResultList = this.queryForList(wSQLText, wParms, wPagination);
            SetValue(wResultList, wQueryResultList);
        } catch (Exception ex) {
            logger.error("DMS_QueryDMSMaintenanceItemListNoTask", ex);
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
        }
        return wResultList;
    }


}
